﻿DailyTrackerComm = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0", "AceComm-2.0")
DailyTrackerComm.CommPrefix = "DAILYQUESTTRACKER";
DailyTrackerComm.CommPrefix_Null = "DQT_COMMPREFIX_NULL";
DailyTrackerComm.CommPrefix_Response = "DQT_COMMPREFIX_RESP";
DailyTrackerComm.CommPrefix_Request = "DQT_COMMPREFIX_REQ";

DailyTrackerComm.RequestQueue = {};

function DailyTrackerComm:SendDailyResponse(questCategoryId, questId)
	DailyTracker:Debug( "Sending daily response:", questCategoryId, questId );
	self:SendCommMessage("GUILD", self.CommPrefix_Response, questCategoryId, questId);
	self:SendCommMessage("WHISPER", UnitName("player"), self.CommPrefix_Response, questCategoryId, questId);
end

function DailyTrackerComm:SendCategoryRequest(...)
	DailyTracker:Debug( "Sending category request:", ... );
	self:SendCommMessage("GUILD", self.CommPrefix_Request, ...);
	self:SendCommMessage("WHISPER", UnitName("player"), self.CommPrefix_Request, ...);
end

function DailyTrackerComm:SendRequestedCategories()
	DailyTracker:Debug( "Sending requested categories..." );
	-- our delay has expired, send any queued requests that havent been answered by someone else
	for i=1,table.getn(self.RequestQueue) do
		local quest = DailyTracker:GetCurrentQuest( self.RequestQueue[i] );
		if quest then
			DailyTracker:Debug( "Response found for " .. self.RequestQueue[i] .. ":", quest.Id );
			self:SendDailyResponse( self.RequestQueue[i], quest.Id );
		end
	end
	
	-- clear requests queue
	self.RequestQueue = {};
end

function DailyTrackerComm:ReceiveCategoryRequest(prefix, sender, distribution, ... )
	DailyTracker:Debug("Category request received for ", ...);
	-- spin through our requests and add them to the request queue
	for i=1,select("#", ...) do
		local dailyName = select(i, ...);
		DailyTracker:Debug("Adding " .. dailyName .. " to queue...");
		table.insert( self.RequestQueue, dailyName );
	end
	
	-- pull a random amount of time between 0-10 seconds for a response
	DailyTracker:Debug("Waiting...");
	DailyTrackerTimer:Set( math.random() * 10, self.SendRequestedCategories, self );
end

function DailyTrackerComm:ReceiveDailyResponse(prefix, sender, distribution, questCategoryId, questId)
	DailyTracker:Debug("Received daily response:", questCategoryId, questId );
	-- remove the category from the queue so we dont send redundant info
	local removeIndex = -1;
	for i=1,table.getn(self.RequestQueue) do
		if questCategoryId == newQueue[i] thenok, ac
			removeIndex = i;
		end
	end
	if removeIndex > 0 then
		DailyTracker:Debug("Found it!  Removing...");
		table.remove(self.RequestQueue, removeIndex);
	end
	
	-- update our current dailies collection
	if questCategoryId and questId then
		quest = DailyTracker:GetQuest( questCategoryId, questId );
		if quest then
			DailyTracker:Debug("Associated quest found.  Setting it up...", quest.Title, quest.Location);
			DailyTracker:FoundQuest( quest.Title, true );
		else
			DailyTracker:Print( "COULD NOT FIND QUEST: ", questCategoryId, questId );
		end
	end
end

function DailyTrackerComm:ReceiveComm(prefix, sender, distribution, localPrefix, ...)
	DailyTracker:Debug("Comm received: ", localPrefix, ...);
	if localPrefix == self.CommPrefix_Response then
		self:ReceiveDailyResponse(prefix, sender, distribution, ...);
	elseif localPrefix == self.CommPrefix_Request then
		self:ReceiveCategoryRequest(prefix, sender, distribution, ...);
	end
end

function DailyTrackerComm:OnEnable()
	self:SetCommPrefix(self.CommPrefix);
	self:RegisterComm(self.CommPrefix, "GUILD", "ReceiveComm");
	self:RegisterComm(self.CommPrefix, "WHISPER", "ReceiveComm");
end
